<?php 
/*
  +----------------------------------------------------------------------+
  | Uploadprogress extension                                             |
  +----------------------------------------------------------------------+
  | Copyright (c) 2006-2008 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt.                                 |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author: Christian Stocker (chregu@php.net)                           |
  +----------------------------------------------------------------------+
*/

$id = md5(microtime() . rand());
//echo $id;
  
function return_bytes($val) {
  $val = trim($val);
  $last = strtolower($val[strlen($val)-1]);
  switch($last) {
    // The 'G' modifier is available since PHP 5.1.0
  case 'g':
            $val *= 1024;
  case 'm':
    $val *= 1024;
        case 'k':
	  $val *= 1024;
  }
  
  return $val;
}
?>
<?php echo $this->render('user/head.phtml');?>
<style>
#progressbar{width: 80%; height: 20px; color: green;}
#status{width: 80%; font-size: 1.2em; }
</style>
<script src="/static/jqueryui/ui/jquery.ui.progressbar.js"></script>
<script src="/static/js/main.js"></script>
<script type="text/javascript">

var UP = function() {
    
    /* private variables */
    
    var ifr = null;
    
    var startTime = null;
    var upload_max_filesize = <?php echo return_bytes(ini_get('upload_max_filesize'));?>;
    
    var infoUpdated = 0;
    
    var convertSpeed = function(speed_bytes){
	if (speed_bytes >= 1024 * 1024)
	{
	    return parseFloat(speed_bytes/(1024*1024)).toFixed(1)+"M";
	}
	else if (speed_bytes >= 1024)
	{
	    return parseFloat(speed_bytes/(1024)).toFixed(1)+"K";
	}
	else
	{
	    return parseFloat(speed_bytes).toFixed(1)+"B";
	}

    }
    var convertTime = function(seconds){
	if (seconds > 24*60*60)
	{
	    return ;
	}
    }
    var writeStatus = function(text,color) {
	text = "上传过程中请不要关闭本页面。<br/>"+text;
        var statDiv = document.getElementById("status");
        if (color == 1 ) {
            statDiv.style.color = "green";
        } else if (color == 2 ) {
            statDiv.style.color= "orange";
        } else if (color == 3 ) {
            statDiv.style.color= "red";
        } else {
            statDiv.style.color= "white";
        }
        statDiv.innerHTML = text;
    }
    
    return {
        start: function() {
           ifr = document.getElementById("ifr");
           startTime = new Date();
           infoUpdated = 0;
           this.requestInfo();
	   jQuery("#progressbar").progressbar({ value: 0 });
	   writeStatus("准备上传", 1);
        },
        stop: function(files) {
           if (typeof files == 'undefined' || files) {
                var secs = (new Date() - startTime)/1000;
                var statusText = "Upload succeeded, it took " + secs + " seconds. <br/> ";
                if (infoUpdated > 0) {
                    //writeStatus(statusText + "You had " + infoUpdated + " updates from the progress meter, looks like it's working fine",1);
                } else {
                    statusText += "BUT there were no progress meter updates<br/> ";
                    if (secs < 3) {
                      //writeStatus(statusText + "Your upload was maybe too short, try with a bigger file or a slower connection",2);
                    } else {
                      //writeStatus(statusText + "Your upload should have taken long enough to have an progress update. Maybe it really does not work...",3);
                    }
                } 
		jQuery("#progressbar").progressbar({ value: 100 });
		writeStatus("上传成功。", 1);
		window.location.href = "/user/upload-success"; 
           } else {
               //writeStatus('PHP did not report any uploaded file, maybe it was too large, try a smaller one (post_max_size: <?php echo ini_get('post_max_size');?>)',3);
           }
           startTime = null;
        },
        requestInfo: function() {
                ifr.src="/user/upload-info?ID=<?php echo $id;?>&"+new Date();
        },
        
        updateInfo: function(uploaded, total, speed_last, estimatedSeconds) {
            if (startTime) {
                if (uploaded) {
                    infoUpdated++;
		    var percent = Math.floor(uploaded / total * 100);
                    if (total > upload_max_filesize) {
                        writeStatus("The file is too large and won't be available for PHP after the upload<br/> Your file size is " + total + " bytes. Allowed is " + upload_max_filesize + " bytes. That's " + Math.round (total / upload_max_filesize * 100) + "% too large<br/> Download started since " + (new Date() - startTime)/1000 + " seconds. " + percent + "% done, " + estimatedSeconds + "  seconds to go",2);
			
                    } else {
                        //writeStatus("Download started since " + (new Date() - startTime)/1000 + " seconds. " + percent + "% done, " + estimatedSeconds + "  seconds to go");
                    }
                    
		    writeStatus("已上传：" + percent + "%, 当前速度：" + convertSpeed(speed_last) + ", 还有 " + estimatedSeconds + "  秒传输完。", 1);
		    jQuery("#progressbar").progressbar({ value: percent});
                } else {
                    //writeStatus("Download started since " + (new Date() - startTime)/1000 + " seconds. No progress info yet");
                }
                window.setTimeout("UP.requestInfo()",1000);
            }
        }
        
        
    }

}()


</script>
<a class="big-text" href="/static/tools/chs_bsvcsetup3.3.zip">下载格式转换工具</a>
<br/>
<form onsubmit="UP.start()" target="ifr2" action="/user/receive-course" enctype="multipart/form-data" method="post">
  <table id='table_upload' class="user_table left">
    <tr class='odd'>
      <td class="td_desc">
	<input type="hidden" name="UPLOAD_IDENTIFIER" value="<?php echo $id;?>" /> 
	<label>选择文件：</label> 
      </td>
      <td>
	<input type="file" name="file_upload" /><br/>
  文件不能超过 <?php echo ini_get('upload_max_filesize');?>，支持持<?php echo FORMAT_SUPPORTED;?>格式；<br/>
	如果不是这种格式，请下载格式转换工具，先转换成这种格式，再上传。
      </td>
    </tr>
    <tr class='odd'>
      <td class="td_desc">标题：</td>
      <td><input id='upload_input_title' type='test' name='title'/></td>
    </tr>
    <tr class='odd'>
      <td class="td_desc" class='odd'>描述：</td>
      <td><textarea id='upload_input_desc' name='descript'></textarea></td>
    </tr>
    
    <tr class='odd'>
      <td class="td_desc">分类：</td>
      <td rolspan="3">
	<?php $i=0; ?>
	<div class="left">
	  <select class="category" name="cat_id_<?php echo $i;?>" id="category_<?php echo $i;?>" index="<?php echo $i++;?>">
      	    <?php print make_options($this->parent_categories);?>
          </select>
	</div>
	<?php for(;$i<5; ){ ?>
	<div class="left">
	  <select class="category" name="cat_id_<?php echo $i;?>" id="category_<?php echo $i;?>" index="<?php echo $i++;?>" style="display: none;">
	  </select>
	</div>
	<?php } ?>
      </td>

      <!-- <td> -->
      <!-- 	选择分类： -->
        <!-- <select id="parent_category"> -->
	<!-- <select class="category" name="cat_id_<?php echo $i;?>" id="category_<?php echo $i;?>" index="<?php echo $i++;?>"> -->
      	<!--   <?php print make_options($this->parent_categories);?> -->
        <!-- </select> -->
      <!-- 	选择科目： -->
      <!--   <select name="cat" id="sub_category"> -->
      <!--   </select> -->
      <!-- </td> -->
    </tr>
    <tr class='odd'>
      <td class="td_desc">
	关键字：
      </td>
      <td id='td_cat_tags'>
	<input type='text' name='tag1' class='input_tag' size='10' value=""/>
	<input type='text' name='tag2' class='input_tag' size='10' value=""/>
	<input type='text' name='tag3' class='input_tag' size='10' value=""/>
      </td>
    </tr>

    <!-- <tr> -->
    <!--   <td> -->
    <!--     <input id="input_cont_tags" name='input_cont_tags' type='hidden'/> -->
    <!--   </td> -->
    <!-- </tr>   -->
    <tr class='odd'>
      <td class="td_desc">
	价格：
      </td>
      <td>
	<input type='text' name='ori_price'/>
      </td>
    </tr>
    <tr class='odd'>
      <td class="td_desc">
	是否促销：
      </td>
      <td>
	<input type='checkbox' name='promotion'/>
      </td>
    </tr>

    <tr class='odd'>
      <td class="td_desc">
	促销价格：
      </td>
      <td>
	<input type='text' name='dis_price'/>
      </td>
    </tr>
    <tr>
      <td class="td_desc">
	&nbsp;
      </td>
      <td>
	<input type="submit" value="上传" />
      </td>
    </tr>
  </table>
  <br/>
    
   <?php
   
  $templateini =  ini_get("uploadprogress.file.filename_template");
  $testid = "thisisjustatest";
  $template = sprintf($templateini,$testid);
  $templateerror = false;
  if ($template && $template != $templateini && @touch ($template) && file_exists($template)) {
        //print '('.$templateini.' is writable. The realpath is ' . str_replace($testid,"%s",realpath($template)) .')';  
        unlink($template);
  } else {
        $templateerror = true;   
  }
  
  ?>
   </form>
   <div id="status" style="border: 0px black solid; <?php
   if (function_exists("uploadprogress_get_info")) {  
       if ($templateerror) {
           //print 'background-color: red;"';
           print ">Problem. ";
           if ($template == $templateini) {
               print "uploadprogress.file.filename_template ($templateini) doesn't have an %s in it for making unique temporary files. Please adjust.<br/>";
           } else {
               print "$templateini is NOT writable. <br/>Please make sure the directory exists and is writable for the webserver. <br/>
               Or adjust the ini setting 'uploadprogress.file.filename_template' to a correct path.";
           }
       } else {
           //print 'background-color: green;">The uploadprogress extension is installed and initial checks show everything is good';
           print '">';
       }
       
       
   } else { ?>
       
       //background-color: red;">The uploadprogress extension is not installed.
       ;">The uploadprogress extension is not installed.
       
       
   <?php } ?>
   
  </div>
  <br/>
  <div id="progressbar" width="200px" height="20px"></div>
   
   <!-- <div>The info during the upload will be displayed here:</div> -->
   <iframe id="ifr" src="/user/upload-info?ID=<?php echo $id;?>" style="display: none" name="ifr"></iframe>
   <!--
   <div>
     The actual file upload happens here (and displays info, when it's finished):
   </div>
   -->
   <iframe name="ifr2" id="ifr2" style="display: none"></iframe> 
<?php echo $this->render('user/foot.phtml');?>
